//
//  THHomeSegeViewController.m
//  TreeHole
//
//  Created by apple on 2020/7/27.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THMajonDetailViewController.h"

#import "THMajorDetailUserInfoViewController.h"
#import "THMajorDetailServiceInfoViewController.h"
#import "THMajorDetailBuyInfoViewController.h"
#import "THMajorDetailCommentViewController.h"
#import "THBabyComitOrderViewController.h"
@interface THMajonDetailViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) STTools_SegementAutoView                     *segView;/**<  */
@property(nonatomic, strong) UIView                     *topview;/**<  */
@property(nonatomic, strong) STButton                     *attentButton;/**<  */
@property(nonatomic, strong) STLabel                     *money_lable;/**<  */
@end

@implementation THMajonDetailViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleDefault;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    [THBabyTeacherDataController send_get_teacher_detail_request:self.userModel.user_id mid:self.userModel.major_id handle:^(THBabyTeacherDetail * _Nonnull model) {
        model.major_name = self.userModel.major_name;
        self.model = model;
        [self stoneobs_ui_configSubView];
        [self configBootomView];
    
    }];
    
    
    // Do any additional setup after loading the view.
}
- (void)stoneobs_fun_st_rightBarAction:(id)sender{
    [THTreeHoleDataController addAttentionOrCancleFormUser_id:self.model.user_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
        self.attentButton.selected = !self.attentButton.selected;
    }];
}
- (void)configBootomView{
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_tabBarTop, STONEOBS_UI_UISCREEN_WIDTH, 49 + 50)];
    [self.view addSubview:bootomView];
    bootomView.backgroundColor = UIColor.whiteColor;
    [bootomView stoneobs_fun_st_showTopShadow];
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(16, 0, 200, 49)
                                                            text:[NSString stringWithFormat:@"￥%0.2f",self.model.price.floatValue]
                                                       textColor:WH_UICOLOR_FROM_RGBA(0xFF2F3D)
                                                            font:15
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [bootomView addSubview:stoneobs_ui_label];
    self.money_lable = stoneobs_ui_label;
    __weak typeof(self) weakSelf =  self;
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(10, 0, 90, 30)
                                                              title:@"立即下单"
                                                         titleColor:UIColor.whiteColor
                                                          titleFont:13
                                                       cornerRadius:15
                                                    backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                                    backgroundImage:nil
                                                              image:nil];
    stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    stoneobs_ui_button.centerY = 24.5;
    [bootomView addSubview:stoneobs_ui_button];
    stoneobs_ui_button.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:stoneobs_ui_button.bounds andColors:@[WH_UICOLOR_FROM_RGBA(0x56BD49),WH_UICOLOR_FROM_RGBA(0x25B13A)]];
    stoneobs_ui_button.right = STONEOBS_UI_UISCREEN_WIDTH - 10;
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {
        
        THBabyComitOrderViewController * vc = THBabyComitOrderViewController.new;
        vc.model = weakSelf.model;
        vc.userModel = weakSelf.userModel;
        [weakSelf pushViewController:vc animated:1];
    }];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    __weak typeof(self) weakSelf =  self;
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 58, 24)
                                                              title:@"+关注"
                                                         titleColor:WH_UICOLOR_FROM_RGBA(0x343434)
                                                          titleFont:12
                                                       cornerRadius:12
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:nil];
    stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {
        
    }];
    [stoneobs_ui_button stbw_setBorderWith:1 borderColor:WH_UICOLOR_FROM_RGBA(0x343434) cornerRadius:12];
    [stoneobs_ui_button setTitle:@"取消关注" forState:UIControlStateSelected];
    [self stoneobs_fun_st_setRightItemWithView:stoneobs_ui_button];
    self.attentButton = stoneobs_ui_button;
    stoneobs_ui_button.selected = self.model.is_follow;
    
    self.title = self.model.real_name;
    UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(0, TMUtils.stoneobs_fun_get_navgationBarBootom, UISCREEN_WIDTH, 300)];
    topView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:topView];
    self.topview = topView;
    
    CGFloat top = 10;
    UIImageView * stoneobs_ui_icon_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 13, 60, 60)];
    [stoneobs_ui_icon_imageView sd_setImageWithURL:self.model.header_img.mj_url];
    [topView addSubview:stoneobs_ui_icon_imageView];
    stoneobs_ui_icon_imageView.layer.cornerRadius = 30;
    stoneobs_ui_icon_imageView.clipsToBounds = YES;
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_icon_imageView.right+20, 24, 300, 25)
                                                                text:self.model.real_name
                                                           textColor:UIColor.blackColor
                                                                font:23
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [topView addSubview: stoneobs_ui_label];
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:23];
        top = stoneobs_ui_label.bottom;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_icon_imageView.right+20, top, 300, 20)
                                                                text:@"品质服务，需要预约"
                                                           textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                font:10
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [topView addSubview: stoneobs_ui_label];
        top = stoneobs_ui_label.bottom;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_icon_imageView.right+20, top, 300, 20)
                                                                text:[NSString stringWithFormat:@"已售%@",self.model.sale_num]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:10
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [topView addSubview: stoneobs_ui_label];
        top = stoneobs_ui_label.bottom;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top+20, 300, 15)
                                                                text:@"星级:"
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:10
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [topView addSubview: stoneobs_ui_label];
        [NSString stoneobs_fun_lableAutoAdjustWitdhWithLabel:stoneobs_ui_label];
        
        STTools_XingXIngView * xingxing = [[STTools_XingXIngView alloc] initWithFrame:CGRectMake(0, 0, 22*5, 22) maxsNum:5];
        [xingxing stoneobs_fun_st_makeXingXingSelectedWithNum:self.model.star.intValue];
        xingxing.userInteractionEnabled = NO;
        [topView addSubview:xingxing];
        xingxing.centerY = stoneobs_ui_label.centerY;
        xingxing.left = stoneobs_ui_label.right + 10;
        
        
        STLabel * stoneobs_ui_c_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, 300, 15)
                                                                  text:[NSString stringWithFormat:@"共%@个消费评价>>",self.model.comment_num]
                                                           textColor:STONEOBS_UI_THIRD_TEXT_COLOR
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentRight];
        [topView addSubview: stoneobs_ui_c_label];
        stoneobs_ui_c_label.right = UISCREEN_WIDTH - 15;
        
        top = stoneobs_ui_label.bottom + 10;
    });
    
    ({
        UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH, 10)];
        stoneobs_ui_line.backgroundColor = WH_UICOLOR_FROM_RGBA(0xF6F6F6);
        [topView addSubview:stoneobs_ui_line];
        top = stoneobs_ui_line.bottom;
    });

    self.segView = [[STTools_SegementAutoView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH , 44) andTitle:@[@"个人资料",@"服务详情",@"购买须知",@"评论"] handle:^(STTools_SegementAutoView *sender, UIButton *currentSelctedButton) {
        
        [weakSelf.scrollView setContentOffset:CGPointMake(sender.cureentIndex*UISCREEN_WIDTH, 0) animated:YES];

    }];
    self.segView.lineSelectedColor = UIColor.clearColor;
    self.segView.lineSelectedView.backgroundColor = UIColor.clearColor;
    self.segView.lineColor = UIColor.clearColor;
    self.segView.autoMoveWithClic = YES;
    
    self.segView.butTitleSelectedColor = UIColor.blackColor;
    self.segView.butTitleColor = STONEOBS_UI_THIRD_TEXT_COLOR;
    self.segView.butTitleFont = [UIFont systemFontOfSize:15];
    self.segView.butTitleSelectedFont = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:17];
    
    UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 21, 10)];
    stoneobs_ui_imageView.image = [UIImage imageNamed:@"椭圆4"];
    [self.segView.lineSelectedView addSubview:stoneobs_ui_imageView];
    stoneobs_ui_imageView.centerX = self.segView.lineSelectedView.width/2;
    stoneobs_ui_imageView.centerY =  self.segView.lineSelectedView.height / 2;
    self.segView.lineSelectedView.clipsToBounds = NO;
    self.segView.lineSelectedView.top = self.segView.lineSelectedView.top-5;
    self.segView.clipsToBounds = NO;

    [topView addSubview:self.segView];
    topView.height = self.segView.bottom;

    self.scrollView = [UIScrollView.alloc initWithFrame:CGRectMake(0, self.topview.bottom, UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_tabBarTop - self.topview.bottom)];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    NSInteger i = 0;
    ({
        THMajorDetailUserInfoViewController * vc = THMajorDetailUserInfoViewController.new;
        vc.model = self.model;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THMajorDetailServiceInfoViewController * vc = THMajorDetailServiceInfoViewController.new;
        vc.model = self.model;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THMajorDetailBuyInfoViewController* vc = THMajorDetailBuyInfoViewController.new;
        vc.model = self.model;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THMajorDetailCommentViewController * vc = THMajorDetailCommentViewController.new;
        vc.model = self.model;
        vc.userModel = self.userModel;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    
    self.scrollView.contentSize = CGSizeMake(UISCREEN_WIDTH * i, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.alwaysBounceVertical = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
@end
